package com.union.utils;

import android.content.Context;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.Window;


import com.union.dialog.BirthDateDialog;

import java.text.SimpleDateFormat;

/**
 * Created by Administrator on 2016/10/26.
 */

public class DateDialogUtils {

    private Context context;
    private String curDate;
    private String dateTime;
    private DataDialogCallBack dialogCallBack;

    public void setDialogCallBack(DataDialogCallBack dialogCallBack) {
        this.dialogCallBack = dialogCallBack;
    }

    /**
     * 选择时间
     *
     * @param context
     * @param type    1时间年份到当前 2 选择时间只显示年月 3全部时间
     * @param from    来自于那个界面
     */
    public void getDate(Context context, int type, String dates, String from) {
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        int width = dm.widthPixels;
        int height = dm.heightPixels;
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateTime = sDateFormat.format(new java.util.Date());
        curDate = dateTime;

        int[] date = getYMDArray(curDate, "-");
        // int[] time = getYMDArray(time_tv.getText().toString(), ":");
        BirthDateDialog birthDiolog = new BirthDateDialog(context,
                new BirthDateDialog.PriorityListener() {
                    @Override
                    public void refreshPriorityUI(String year, String month,
                                                  String day) {
                        if (dialogCallBack != null) {
                            dialogCallBack.selectData(year, month, day);
                        }
                    }
                }, date[0], date[1], date[2], width, height, "", type, from);
        Window window = birthDiolog.getWindow();
        window.setGravity(Gravity.BOTTOM); // 此处可以设置dialog显示的位置
        birthDiolog.setCancelable(true);
        birthDiolog.show();
    }

    /**
     * 时间截取
     *
     * @param datetime
     * @param splite
     * @return
     */
    public static int[] getYMDArray(String datetime, String splite) {
        int date[] = {0, 0, 0, 0, 0};
        if (datetime != null && datetime.length() > 0) {
            String[] dates = datetime.split(splite);
            int position = 0;
            for (String temp : dates) {
                date[position] = Integer.valueOf(temp);
                position++;
            }
        }
        return date;
    }

    public interface DataDialogCallBack {
        void selectData(String year, String month, String day);
    }
}
